package com.fbty.mt.service.impl;

import com.fbty.base.dao.MtJdbcDao;
import com.fbty.base.domain.Page;
import com.fbty.mt.dao.RecordDao;
import com.fbty.mt.domain.Record;
import com.fbty.mt.service.RecordService;

public class RecordServiceImpl implements RecordService {
	
	private MtJdbcDao mtJdbcDao;
	
	private RecordDao recordDao;
	
	@Override
	public int getNum(int uid) {
		String sql = " SELECT SUM(p.num) AS total FROM tab_price p,tab_record r "+
					 " WHERE r.pid=p.id AND r.state=1 "+
					 " AND r.uid=?";
		int num = mtJdbcDao.getJdbcTemplate().queryForInt(sql,new Object[]{uid});
		
		return num;
	}
	
	public int getUseTime(int uid){
		String sql =" SELECT useTime FROM tab_price p,tab_record r  "+
				    " WHERE r.pid=p.id AND r.buyDate = (SELECT * FROM (SELECT MIN(tr.buyDate)"+ 
				    " FROM tab_record tr,tab_price tp WHERE tr.uid=? AND tr.pid=tp.id " +
				    " AND tp.type=3 AND tr.state=1) t)"+
				    " AND r.uid=? AND p.type=3";
		int num = mtJdbcDao.getJdbcTemplate().queryForInt(sql,new Object[]{uid,uid});
		
		return num;
	}

	@Override
	public void deleteRecord(int rid) {
		// TODO Auto-generated method stub

	}

	@Override
	public void saveRecord(Record record) {
		// TODO Auto-generated method stub

	}

	@Override
	public Page queryRecords() {
		// TODO Auto-generated method stub
		return null;
	}

	public void setMtJdbcDao(MtJdbcDao mtJdbcDao) {
		this.mtJdbcDao = mtJdbcDao;
	}

	public void setRecordDao(RecordDao recordDao) {
		this.recordDao = recordDao;
	}

	@Override
	public int getOnlyNum(int uid) {
		String sql ="SELECT COUNT(r.pid) FROM tab_record r,tab_price p WHERE uid=? AND r.pid=p.id AND p.type=3 AND r.state=1";
		int num = mtJdbcDao.getJdbcTemplate().queryForInt(sql,new Object[]{uid});
		return num;
	}
	
	

}
